home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / wstype / source / optparse.c < prev    next >
C/C++ Source or Header  |  1991-10-18  |  4KB  |  228 lines

  1. /***   [optparse.c]
  2. *
  3. *    起動時オプション解析        (C)ささがわ
  4. *
  5. *    For GNU C Compiler (GCC)   Version 1.39
  6. *
  7. ***/
  8.  
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "others.h"
  12.  
  13. #define OPT        '/'
  14. #define OPTN    15
  15.  
  16. static int    argc;
  17. static char    **argv;
  18. static const char    *opt_name[OPTN] = {
  19.     "tabm", "crm", "tab", "lg", "lc",
  20.     "bg-tiff", "bg-tile", "bg-beta",
  21.     "wa-beep", "wa-pcm", "m",
  22.     "fcap", "fsort", "fwide", "scspd"
  23. };
  24. static const char    opt_n[OPTN] = {
  25.     1, 1, 1, 1, 1,
  26.     1, 2, 1,
  27.     0, 1, 0,
  28.     0, 1, 0, 1
  29. };
  30.  
  31. static int Next(int);
  32.  
  33. void OPT_init(int ac, char *av[]) {
  34.     argc = ac;
  35.     argv = av;
  36. }
  37.  
  38. static int Next(int finit) {
  39.     int        i, r;
  40.     char    *p;
  41.     static int    no;
  42.     
  43.     if (finit) {
  44.         no = 1;
  45.         return 0;
  46.     } else if ((r = no++) >= argc)
  47.         return -1;
  48.     
  49.     if (argv[r][0] == OPT && strlen(argv[r]) >= 2) {
  50.         p = &argv[r][1];
  51.         for (i = 0; i < OPTN; i++) {
  52.             if (!stricmp(p, opt_name[i])) {
  53.                 no += opt_n[i];
  54.                 break;
  55.             }
  56.         }
  57.     }
  58.     
  59.     if (no - 1 >= argc)
  60.         return -1;
  61.     else
  62.         return r;
  63. }
  64.  
  65. void OPT_filew(char *wide, char *cap, char *sort) {
  66.     int        i;
  67.     char    *p;
  68.     
  69.     *wide = 0;
  70.     *cap = 0;
  71.     *sort = 0;
  72.     Next(1);
  73.     while ((i = Next(0)) >= 0) {
  74.         if (argv[i][0] != OPT || strlen(argv[i]) < 2)
  75.             continue;
  76.         
  77.         p = &argv[i][1];
  78.         if (!stricmp(p, opt_name[11])) {
  79.             *cap = 1;
  80.         } else if (!stricmp(p, opt_name[12])) {
  81.             *sort = atoi(argv[i + 1]);
  82.             if (*sort < 0 || 3 < *sort)
  83.                 *sort = 0;
  84.         } else if (!stricmp(p, opt_name[13])) {
  85.             *wide = 1;
  86.         }
  87.     }
  88.     
  89.     return;
  90. }
  91.  
  92. int OPT_BackG(char *path, int *no) {
  93.     int        i, ret = -1;
  94.     char    *p;
  95.     
  96.     path[0] = '\0';
  97.     *no = 0;
  98.     Next(1);
  99.     while ((i = Next(0)) >= 0 && ret < 0) {
  100.         if (argv[i][0] != OPT || strlen(argv[i]) < 2)
  101.             continue;
  102.         
  103.         p = &argv[i][1];
  104.         if (!stricmp(p, opt_name[5])) {
  105.             ret = 0;
  106.             strncpy(path, argv[i + 1], 89);
  107.             break;
  108.         } else if (!stricmp(p, opt_name[6])) {
  109.             ret = 1;
  110.             strncpy(path, argv[i + 1], 89);
  111.             *no = atoi(argv[i + 2]);
  112.             break;
  113.         } else if (!stricmp(p, opt_name[7])) {
  114.             ret = 2;
  115.             *no = atoi(argv[i + 1]);
  116.             break;
  117.         }
  118.     }
  119.     
  120.     path[89] = '\0';
  121.     if (ret < 0)
  122.         ret = 2;
  123.     return ret;
  124. }
  125.  
  126. int OPT_WarnS(char *path) {
  127.     int        i, ret = 0;
  128.     char    *p;
  129.     
  130.     path[0] = '\0';
  131.     Next(1);
  132.     while ((i = Next(0)) >= 0 && ret == 0) {
  133.         if (argv[i][0] != OPT || strlen(argv[i]) < 2)
  134.             continue;
  135.         
  136.         p = &argv[i][1];
  137.         if (!stricmp(p, opt_name[9])) {
  138.             ret = 1;
  139.             strncpy(path, argv[i + 1], 89);
  140.             break;
  141.         } else if (!stricmp(p, opt_name[8])) {
  142.             ret = 2;
  143.             break;
  144.         }
  145.     }
  146.     path[89] = '\0';
  147.     
  148.     return ret;
  149. }
  150.  
  151. int OPT_Cons(void) {
  152.     int        i, r = 0;
  153.     
  154.     Next(1);
  155.     while ((i = Next(0)) >= 0) {
  156.         if (argv[i][0] == OPT && strlen(argv[i]) >= 2 && !stricmp(&argv[i][1], opt_name[10])) {
  157.             r = 1;
  158.             break;
  159.         }
  160.     }
  161.     
  162.     return r;
  163. }
  164.  
  165. int OPT_Scroll(void) {
  166.     int        i, r = -1;
  167.     
  168.     Next(1);
  169.     while ((i = Next(0)) >= 0) {
  170.         if (argv[i][0] == OPT && strlen(argv[i]) >= 2 && !stricmp(&argv[i][1], opt_name[14])) {
  171.             r = atoi(argv[i + 1]);
  172.             break;
  173.         }
  174.     }
  175.     
  176.     return r;
  177. }
  178.  
  179. int OPT_txt_n(void) {
  180.     int        i, ret = 0;
  181.     
  182.     Next(1);
  183.     while ((i = Next(0)) >= 0) {
  184.         if (argv[i][0] != OPT)
  185.             ret++;
  186.     }
  187.     Next(1);
  188.     
  189.     return ret;
  190. }
  191.  
  192. const char *OPT_txt(void) {
  193.     int        i;
  194.     const char    *ret = NULL;
  195.     
  196.     while ((i = Next(0)) >= 0) {
  197.         if (argv[i][0] != OPT) {
  198.             ret = argv[i];
  199.             break;
  200.         }
  201.     }
  202.     
  203.     return ret;
  204. }
  205.  
  206. void OPT_disp(short *par) {
  207.     int        i;
  208.     char    *p;
  209.     
  210.     Next(1);
  211.     while ((i = Next(0)) >= 0) {
  212.         int        a;
  213.         
  214.         if (argv[i][0] != OPT || strlen(argv[i]) < 2)
  215.             continue;
  216.         
  217.         p = &argv[i][1];
  218.         for (a = 0; a < 5; a++) {
  219.             if (!stricmp(p, opt_name[a])) {
  220.                 par[a] = atoi(argv[i + 1]);
  221.                 break;
  222.             }
  223.         }
  224.     }
  225.     
  226.     return;
  227. }
  228.